import { BaseEntity } from "./BaseEntity";
import * as Cesium from "cesium";
import type {
  AircraftEntityOptions,
  WcsPath,
  WgsPath,
} from "../interfaces/entity";
import { CesiumUtils } from "../utils/CesiumUtils";
/**
 * 雷达实体（继承基类）
 */
export class AircraftEntity extends BaseEntity {
  private _options: AircraftEntityOptions;
  private _positionProperty = new Cesium.SampledPositionProperty();
  constructor(options: AircraftEntityOptions) {
    super(options);
    this._options = options;
    this.setAppearance();
  }

  // 实现抽象方法 - 雷达外观
  protected setAppearance(): void {
    this.setModel();
    this.loadWgsPaths(this._options.wgsPaths);
  }

  private setModel() {
    if (!this._options.Model) return;
    const { src, minimumPixelSize } = this._options.Model;

    this._entity.model = new Cesium.ModelGraphics({
      uri: src,
      minimumPixelSize,
    });
  }

  loadWcsPaths(_wcsPaths: WcsPath[]) { }
  loadWgsPaths(wgsPaths?: WgsPath[]) {
    if (!wgsPaths) return;

    const time = this.viewer?.clock.startTime || Cesium.JulianDate.now();

    this._positionProperty = new Cesium.SampledPositionProperty();
    this._entity.position = this._positionProperty;
    this._entity.orientation = new Cesium.VelocityOrientationProperty(
      this._positionProperty
    );

    (async () => {
      // 创建所有异步操作的 Promise 数组
      const promises = wgsPaths.map(({ t, ...arg }) => {
        const _t = Cesium.JulianDate.addSeconds(
          time,
          t / 1000,
          new Cesium.JulianDate()
        );
        this._positionProperty.addSample(_t, CesiumUtils.wgs84ToWorld(arg));
      });

      // 等待所有 Promise 完成
      await Promise.all(promises);
    })();

    this._entity.path = new Cesium.PathGraphics({
      show: true,
      material: Cesium.Color.BEIGE,
    });
  }
}
